/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.peng.commdit.rest;

import me.peng.annotation.Log;
import me.peng.commdit.domain.CommodityManage;
import me.peng.commdit.service.CommodityManageService;
import me.peng.commdit.service.dto.CommodityManageQueryCriteria;
import me.peng.domain.config.SysResult;
import me.peng.rest.FileController;
import me.peng.service.impl.FileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import me.peng.annotation.rest.AnonymousDeleteMapping;
import me.peng.annotation.rest.AnonymousGetMapping;
import me.peng.annotation.rest.AnonymousPostMapping;
import me.peng.annotation.rest.AnonymousPutMapping;
/**
 * @website https://el-admin.vip
 * @author pengjinaye
 * @date 2021-10-02
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "api/commdit管理")
@RequestMapping("/api/commodityManage")
public class CommodityManageController {

    private final CommodityManageService commodityManageService;



    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('commodityManage:list')")
    public void download(HttpServletResponse response, CommodityManageQueryCriteria criteria) throws IOException {
        commodityManageService.download(commodityManageService.queryAll(criteria), response);
    }

    //    @GetMapping
    @AnonymousGetMapping
    @Log("查询api/commdit")
    @ApiOperation("查询api/commdit")
//    @PreAuthorize("@el.check('commodityManage:list')")
    public ResponseEntity<Object> query(CommodityManageQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(commodityManageService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    public void addImages(CommodityManage resources){
        //商品封面的图片新增
        if (FileController.urlPath!=null){
            resources.setProduconver(FileController.urlPath);
            FileController.urlPath=null;
        }
        //商品轮播的新增
        if (FileController.commdityCarousel.length()!=0){
            resources.setCommcarousel(FileController.commdityCarousel);
            FileController.commdityCarousel="";
        }
        //商品详情图片的新增
        if (FileController.commdityDtails.length()!=0){
            resources.setDetailsMap(FileController.commdityDtails);
            FileController.commdityDtails="";
        }
    }

    @PostMapping
    @Log("新增api/commdit")
    @ApiOperation("新增api/commdit")
    @PreAuthorize("@el.check('commodityManage:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody CommodityManage resources){

        addImages(resources);

        return new ResponseEntity<>(commodityManageService.create(resources),HttpStatus.CREATED);
    }

    @AnonymousPutMapping
    @Log("修改api/commdit")
    @ApiOperation("修改api/commdit")
//    @PreAuthorize("@el.check('commodityManage:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody CommodityManage resources){
        if (resources.getProduconver()!=null&&FileController.urlPath!=null){
            //删除封面图片资源
            FileServiceImpl.deleteFileData(resources.getProduconver());
        }
        //删除轮播
        if (resources.getCommcarousel()!=null&&FileController.commdityCarousel.length()!=0){
            String[] carouse=resources.getCommcarousel().split(",");
            for (String s:carouse){
                FileServiceImpl.deleteFileData(s);
            }
        }
        //删除详情
        if (resources.getDetailsMap()!=null&&FileController.commdityDtails.length()!=0) {
            String[] details = resources.getDetailsMap().split(",");
            for (String s : details) {
                FileServiceImpl.deleteFileData(s);
            }
        }

        addImages(resources);//新增
        commodityManageService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除api/commdit")
    @ApiOperation("删除api/commdit")
    @PreAuthorize("@el.check('commodityManage:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
        commodityManageService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }



}
